﻿using System;
using System.Collections.Generic;
using System.Text;
using EmbeddedSharp;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Core.Function.String {

    /// <summary>
    /// Replace pattern in the string.
    /// 
    /// Syntax: Replace(expr, from, to);
    /// 
    /// Example:
    /// expr = Replace("Hello, world!", "world", "Jane"); # Hello, Jane!
    /// </summary>
    public class ReplaceNode : FuncNode {

        public ReplaceNode()
            : base() {
            base.Value = "replace";
        }

        public override object Evaluate() {
            int length = Parameters.Count;
            if (length < 3) {
                throw new ArgumentException(
                    "Usage: replace(expr, from, to)");
            }

            string expr = Convert.ToString(
                Parameters[length - 1].Evaluate());
            string from = Convert.ToString(
                Parameters[length - 2].Evaluate());
            string to = Convert.ToString(
                Parameters[length - 3].Evaluate());

            return expr.Replace(from, to);
        }
    }
}
